/**
 * 认证相关 Hook
 */

import { computed } from 'vue'
import { useUserStore } from '@/stores/user'
import { AuthService } from '@/services'

export function useAuth() {
  const userStore = useUserStore()

  // 是否已登录
  const isLoggedIn = computed(() => userStore.isLoggedIn)

  // 用户信息
  const userInfo = computed(() => userStore.userInfo)

  // 用户权限
  const permissions = computed(() => userStore.permissions)

  // 检查权限
  const hasPermission = (permission: string) => {
    return AuthService.hasPermission(permission)
  }

  // 检查多个权限（需要全部拥有）
  const hasAllPermissions = (permissions: string[]) => {
    return AuthService.hasAllPermissions(permissions)
  }

  // 检查多个权限（拥有任意一个即可）
  const hasAnyPermission = (permissions: string[]) => {
    return AuthService.hasAnyPermission(permissions)
  }

  // 登录
  const login = async (loginForm: any) => {
    return await userStore.login(loginForm)
  }

  // 登出
  const logout = async () => {
    return await userStore.logout()
  }

  // 获取用户信息
  const getUserInfo = async () => {
    return await userStore.getUserInfo()
  }

  return {
    isLoggedIn,
    userInfo,
    permissions,
    hasPermission,
    hasAllPermissions,
    hasAnyPermission,
    login,
    logout,
    getUserInfo
  }
}
